package com.manli.api.service;

import com.manli.api.pojo.QueryParamsQiniuFile;
import com.manli.api.util.ObjectEmptyUtil;
import com.manli.common.utils.qiniu.QueryParamsFileKey;
import com.manli.common.utils.qiniu.UploadFilesUtil;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.AQiniuFilesDao;
import com.manli.api.dto.AQiniuFiles;


@Service("aQiniuFilesService")
public class AQiniuFilesService {
    @Autowired
    AQiniuFilesDao aQiniuFilesDao;
    @Autowired
    SQLManager sqlManager;

    
    public Result queryPage(QueryParamsQiniuFile params) {
        /**
        * sql中表名需要修改
        */
        StringBuffer sql = new StringBuffer("select * from a_qiniu_files where status=1 ");
        sql.append(StringUtils.isEmpty(params.getFileName())?"":" and file_name like '%" + params.getFileName() + "%'");

        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<AQiniuFiles> pageQuery = new PageQuery<AQiniuFiles>(params.getPage(),params.getLimit());
        PageQuery<AQiniuFiles> query =sqlManager.execute(sqlReady,AQiniuFiles.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    
    public Result queryAll() {
        List<AQiniuFiles> list = aQiniuFilesDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer id) {
        AQiniuFiles aQiniuFiles = aQiniuFilesDao.single(id);
        return ResultUtil.success(aQiniuFiles);
    }

    @Transactional
    public Result save(AQiniuFiles aQiniuFiles) {
        ObjectEmptyUtil.adverIsEmpty(aQiniuFiles);

        List<AQiniuFiles> list = aQiniuFilesDao.execute("select * from a_qiniu_files where file_name = ? and status = 1",aQiniuFiles.getFileName());
        if(list.size() > 0){
            return ResultUtil.error(-1,"文件名已存在");
        }

        aQiniuFiles.setAddTime(new Date());
        aQiniuFiles.setStatus(1);
        aQiniuFilesDao.insert(aQiniuFiles);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(AQiniuFiles aQiniuFiles) {
        ObjectEmptyUtil.adverIsEmpty(aQiniuFiles);
        aQiniuFilesDao.updateTemplateById(aQiniuFiles);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(AQiniuFiles aQiniuFiles) {
        aQiniuFilesDao.updateTemplateById(aQiniuFiles);
        return ResultUtil.success();
    }

    @Transactional
    public Result removeByIds(List<Integer> ids) {
        String sql = "update a_qiniu_files set status = 0 where id = ?";
        for (Integer id:ids) {
            aQiniuFilesDao.executeUpdate(sql,id);
        }
        return ResultUtil.success();
    }

    @Transactional
    public Result deleteByNames(List<String> names) throws Exception{
        String sql = "update a_qiniu_files set status = 0 where file_name = ?";
        for (String name:names) {
            aQiniuFilesDao.executeUpdate(sql,name);
            QueryParamsFileKey queryParamsFileKey = new QueryParamsFileKey();
            queryParamsFileKey.setFileKey(name);
            UploadFilesUtil.deleteFile(queryParamsFileKey);
        }
        return ResultUtil.success();
    }



}
